<?php
use Home\Model\MemberModel;
use Home\Model\RecycleOrderModel;

$m = 0;
/**
 * 自动指派工程师
 * @param  array $type 工程师类型，3-自有工程师，4-滴滴工程师，5-联盟商户工程师
 * @return int   成员id
 */
function assgin_engineer($type){
	$Member = new MemberModel();
	$RecycleOrder = new RecycleOrderModel();
	global $m;
	if(!isset($m)){
		$m = 0;
	}
	$engineer_list = $Member->get_member_list(0,null,$type[$m],FALSE,1);
	$i = 0;
	foreach ($engineer_list as $key => $value) {
		$recycle_order_list = $RecycleOrder->get_today_order($value['member_id']);
		if(count($recycle_order_list) < 5){
			if($recycle_order_list[count($recycle_order_list)-1]['update_time'] < date('Y-m-d H:i:s',strtotime('-1 hour'))){
				$engineer[$i] = $value['member_id'];
				$i++;
			}
		}
	}
	if(!empty($engineer)){
		$i = rand(0,count($engineer)-1);
		return $engineer[$i];
	}else{
		$m++;
		assgin_engineer($type);
	}
}

/**
 * post请求
 * @param  string $url  地址
 * @param  array $postData 请求参数
 * @return string       请求返回数据
 */
function httpPost($url,$postData){

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}

/**
 * 短信验证码接口
 * @param  string $function 参数名称
 * @param  array $postData  参数数组
 * @return json             返回值
 */
function msgcode($function,$postData){
	$url = 'http://sms.txiaoyuan.cn/index.php/Home/Control/control?app=SMS&function='.$function;
	return httpPost($url,$postData);
}

/**
 * 发送短信验证码
 * @param  int $uid      用户id
 * @param  string $mobphone 手机号
 * @return json           返回值
 */
function send_msgcode($mobphone){
	$postData = array();
	$postData['uid'] = 2;
	$postData['user_key'] = 'efcd9440dde0644df6b2cb1bcf1fc549';
	$postData['param_json'] = json_encode(array(
			'sign_name' => 'GO机网',//模板签名
			'product' => 'GO机网',//短信参数
			'template_code' => 'SMS_23910016',//短信模板参数
			'phone' => $mobphone//发送的手机号码
	));
	return msgcode('autoSendSMS',$postData);
}

/**
 * 获取短信验证码
 * @param  int $uid     用户id
 * @param  string $mobphone 手机号
 * @return string       验证码
 */
function get_msgcode($mobphone){
	$postData = array();
	$postData['uid'] = 2;
	$postData['user_key'] = 'efcd9440dde0644df6b2cb1bcf1fc549';

	$postData['param_json'] = json_encode(array(
			'time' => '3600',//时间有效期单位秒
			'phone' => $mobphone//发送的手机号码
	));
	$data = json_decode(msgcode('getCheckSmsCode',$postData),true);
	if($data['code']==200){
		return $data['data']['code'];
	}else{
		return 0;
	}

}


/**
 * 判断手机号验证码
 * @param $phone
 * @param $checkCode
 * @return bool
 */

function verification_code_is_correct($phone,$checkCode){
	$postData = array();
	$postData['uid'] = 2;
	$postData['user_key'] = 'efcd9440dde0644df6b2cb1bcf1fc549';

	$postData['param_json'] = json_encode(array(
			'time' => '3600',//时间有效期单位秒
			'phone' => $phone//发送的手机号码
	));

	$data = httpPost('http://sms.txiaoyuan.cn/index.php/Home/Control/control?app=SMS&function=getCheckSmsCode',$postData);
	$data = json_decode($data,true);
	if($data['code']==200){
		if($data['data']['code']==$checkCode){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}

}

